﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class DonDatHang : System.Web.UI.Page
    {
        private static int[] idgh = new int[100];
        private static int tong = 0;
        private decimal total = 0; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGioHang();
                loadNB();
            }
        }
        protected void loadGioHang()
        {
            MasterPage mt = this.Master as MasterPage;
            Control uc = mt.FindControl("UcDanhMuc1") as Control;
            uc.Visible = true;
            Control uc2 = mt.FindControl("UcDanhMuc2") as Control;
            uc2.Visible = false;

            Page.Title = "Đơn đặt hàng của bạn - Văn phòng phẩm Mỹ Đình";
            DataTable dt = new DataTable();
            if (Session["IDDN"]!=null)
            {
                dt = new cmsDonHangBL().SelectByUserID(int.Parse(Session["IDDN"].ToString()));
            }
            else
            {
                Ultility.Alert("Bạn phải đăng nhập mới xem được chức năng này!", "Default.aspx");
                //Xem các đơn đặt hàng theo sessionID
                //HttpSessionState ss = HttpContext.Current.Session;
                //string session = ss.SessionID;
                //dt = new cmsDonHangBL().SelectBySessionID(session);
            }

            if (dt.Rows.Count > 0)
            {
                GridGioHang.DataSource = dt;
                GridGioHang.DataBind();
            }
            else
            {
                lbl.Text = "Bạn chưa có đơn đặt hàng nào!!<br />Hãy nhấn vào <a href='/ThanhToan.aspx' >Đây </a> để đặt hàng các sản phẩm có trong giỏ hàng của bạn!";
                //   btnCapNhat.Visible = false;
            }
        }
        protected void GridGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridGioHang.PageIndex = e.NewPageIndex;
            loadGioHang();
        }

        protected void GridGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lbSL = (Label)e.Row.FindControl("txtSL");
                    Label DG = (Label)e.Row.FindControl("lbDG");
                    Label lbGG = (Label)e.Row.FindControl("lbGG");

                    int sl = int.Parse(lbSL.Text);
                    decimal dg = Decimal.Parse(DG.Text);
                    int gg = int.Parse(lbGG.Text);

                    Label TT = (Label)e.Row.FindControl("lbTT");
                    TT.Text = (sl * (dg - ((dg*gg)/100))).ToString("##,## VNĐ");

                    DG.Text = dg.ToString("##,## VNĐ");
                }

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
                    e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#DEE0BF'");
                    e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");
                }
            }
            //catch { }

        }
        protected void GridGioHang_DataBinding(object sender, System.EventArgs e)
        {
            //Label pLabel = (Label)sender;
            //int dItemPrice = (Eval("ThanhTien") != DBNull.Value) ? (int)(Eval("ThanhTien")) : 0;
            //pLabel.Text = dItemPrice.ToString("0.00");
            //tong += dItemPrice; // This will add to running total of all.
        }

        protected void GridGioHang_DataBound(object sender, EventArgs e)
        {

        }

        public void loadNB()
        {
            bindatalist(new cmsSanPhamBL().SelectAllBanChay());
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 6;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNB();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNB();
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsGioHangDO obj = new cmsGioHangBL().SelecBySessionAndSPNonXL(session, id);
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                if (obj.GioHangID < 1)
                {
                    obj = init();
                    obj.SessionID = session;
                    obj.SanPhamID = id;
                    if (Session["IDDN"] == null)
                        obj.ClientLoginID = 0;
                    else
                        obj.ClientLoginID = int.Parse(Session["IDDN"].ToString());

                    obj.ThanhTien = gia * obj.SoLuong;
                    new cmsGioHangBL().Insert(obj);
                    Ultility.Alert("Đặt mua thành công!", url);
                }

                else
                {
                    obj.SoLuong++;
                    obj.ThanhTien += gia;
                    new cmsGioHangBL().Update(obj);
                    Ultility.Alert("Đặt mua thành công!", url);
                }
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
        }
    }
}
